<?php

namespace App\Http\Controllers\background;
use App\Http\Controllers\Controller;
use App\Models\Mysql\{XnLogo,XnSupplierStatus};
use App\Models\Admin\{PlatformFeeRechargeRecord, SupplierBalanceChangeRecord, Supplier};
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class AccountController extends Controller
{
    public function accountinfo(Request $request){
        $id = $request -> post('supplier_id');
        if(empty($id)){
            return fails('请传参数');
        }
        $res = Supplier::where('id','=',$id)
            ->select('business_number','company_name')
            -> first();
        if(empty($res)){
            return fails('暂无数据');
        }
        $img = XnLogo::where('supplier_id','=',$id) -> latest('ctime')->value('logo');
        $res['logo'] = '';
        if(!empty($img)){
            $res['logo'] = $request->schemeAndHttpHost().$img;
        }
        return successly($res);
    }

    public function uploadlogo(Request $request){
                        // 1、获取上传的文件
            $file=$request->file('file');

            $supplier_id = $request -> header('supplierId');
            $type = $request -> header('Type');

            //dd($file);exit;
            if(empty($file)){
                return fails('请上传');
            }
            // 3、获取上传文件的后缀（如abc.png，获取到的为png）
            $fileextension=$file->getClientOriginalExtension();
             // 4、获取上传文件的大小
            $filesize=$file->getSize()/1000;
            if($filesize >=2000){
                return fails('文件太大');
            }
            // 6、获取上传的文件缓存在tmp文件夹下的绝对路径
            $realpath=$file->getRealPath();
            /**
             * 7、将缓存在tmp目录下的文件移到某个位置，返回的是这个文件移动过后的路径
             * move()方法有两个参数，第一个参数是文件移到哪个文件夹下的路径，第二个参数是将上传的文件重新命名的文件名
             */
            $storage_path = public_path('uploads/logo'); //public 文件夹下面建 storage/uploads 文件夹
            $filenewname=date('YmdHis').rand(000,999).'.'.$fileextension;

            // 8、检测上传的文件是否合法，返回值为true或false
            if($file->isValid())
            {
                // 9、存储文件，disk里面的willem里面。总的来说就是调用disk模块里的public配置。
                Storage::disk('public')->put($filenewname, file_get_contents($realpath));
                // 生成文件地址：textlaravel.cc\laravel5\storage\app\public\f2cde11.png
                $file->move($storage_path,$filenewname);
                $path = '/uploads/logo/'.$filenewname;
                if($type == 1){
                    XnLogo::insert(['logo'=>$path,'ctime'=>date('Y-m-d H:i:s',time()),'supplier_id'=>$supplier_id]);
                }
                return successly($request->schemeAndHttpHost().$path);
            }else{
                return fails('图片不合法');
            }
    }

    public function accountbal(Request $request){
        $supplier_id = $request -> post('supplier_id','');
        if(empty($supplier_id)){
            fails('请传参');
        }
        $info = Supplier::where('id','=',$supplier_id)
            -> select('platform_fees','platform_fee_expiration_time','register_time')
            -> find($supplier_id)
            -> toArray();
        $start_time = PlatformFeeRechargeRecord::where('sid','=',$supplier_id) -> oldest('add_money_time')->value('aging_start_time') ?? time();
        $info['platform_fee_expiration_time'] = date('Y-m-d',$info['platform_fee_expiration_time']);
        $info['start_time'] = date('Y-m-d',$start_time);
        return successly($info);
    }

    public function accountbill(Request $request){
        $before_atime = $request-> post('before_atime','');
        $after_atime = $request-> post('after_atime','');
        $order_no = $request-> post('order_no','');
        $supplier_id = $request -> post('supplier_id','');
        if(empty($supplier_id)){
            return fails('请传参');
        }
        $condition = [];
        if(!empty($order_no)){
            $condition[] = ['order_no','=',$order_no];
        }
        if(!empty($before_atime) && !empty($after_atime)){
            $condition[] = ['add_money_time','>=',$before_atime];
            $condition[] = ['add_money_time','<=',$after_atime];
        }

        $supplierInfo = Supplier:: find($supplier_id);
        if(empty($supplierInfo)){
            return fails('未查询到供应商数据');
        }
        $infos = $supplierInfo -> account()
            -> where($condition)
            -> select('order_no','add_money_time','money','aging_start_time','aging_end_time')
            -> orderBy('add_money_time','desc')
            -> paginate(10)
            -> toArray();

        if(empty($infos['data'])){
            return fails('未查询到数据');
        }
        $time = time();
        foreach($infos['data'] as $k => $v){
            $infos['data'][$k]['supplier_name'] = $supplierInfo -> company_name;
            $infos['data'][$k]['aging_start_time'] = date('Y-m-d',$v['aging_start_time']);
            $infos['data'][$k]['aging_end_time'] = date('Y-m-d',$v['aging_end_time']);
            if($v['aging_end_time'] <= $time){
                $infos['data'][$k]['use_status'] = 1;
            }else if($v['aging_end_time'] > $time){
                $infos['data'][$k]['use_status'] = 2;
            }
        }
        return successly($infos);
    }

    public function balancebill(Request $request){
        $before_atime = $request-> post('before_atime','');
        $after_atime = $request-> post('after_atime','');
        $order_no = $request-> post('order_no','');
        $supplier_id = $request -> post('supplier_id','');
        $type = $request -> post('type','');
        if(empty($supplier_id)){
            return fails('请传参');
        }
        $condition = [];
        if(!empty($order_no)){
            $condition[] = ['order_no','=',$order_no];
        }
        if(!empty($before_atime) && !empty($after_atime)){
            $condition[] = ['create_time','>=',$before_atime];
            $condition[] = ['create_time','<=',$after_atime];
        }
        if(!empty($type)){
            $condition[] = ['type','=',$type];
        }
        $infos = SupplierBalanceChangeRecord::where($condition)
            -> select('create_time','order_no','type','before_money','money','after_money')
            -> orderBy('create_time','desc')
            -> paginate(10)
            -> toArray();
        return successly($infos);
    }

    public  function balancewarn(Request $request){
        $supplier_id = $request -> post('supplier_id');
        $status_info = XnSupplierStatus::where('supplier_id','=',$supplier_id) -> select('status','time_status','balance_warn') -> first();
        if(!$status_info){
            return fails('未查询到数据');
        }
        $status_info = $status_info -> toArray();
        return successly($status_info);
    }

    public  function fitbalancewarn(Request $request){
        $supplier_id = $request -> post('supplier_id');
        $status = $request -> post('status');
        XnSupplierStatus:: updateOrInsert(['supplier_id'=>$supplier_id],['time_status'=>$status]);
        return successly();
    }
    public  function supplierbalancewarn(Request $request){
        $supplier_id = $request -> post('supplier_id');
        $money = $request -> post('money');
        XnSupplierStatus:: updateOrInsert(['supplier_id'=>$supplier_id],['balance_warn'=>$money]);
        return successly();
    }
}
